home *** CD-ROM | disk | FTP | other *** search
- // Data.h
-
- #ifndef Data_h
- #define Data_h
-
- #ifndef Integers_h
- #include "Integers.h"
- #endif
- #ifndef DebugMessage_h
- #include "DebugMessage.h"
- #endif
- #ifndef Assert_h
- #include "Assert.h"
- #endif
- #ifndef Range_h
- #include "Range.h"
- #endif
- #ifndef ConstArrayOf_h
- #include "ConstArrayOf.h"
- #endif
- #ifndef ConstData_h
- #include "ConstData.h"
- #endif
- #ifndef ArrayOf_h
- #include "ArrayOf.h"
- #endif
-
- // The includes above have been flattened so that weak compilers
- // can cope better.
-
- /*
- A Data is a range of bytes, starting at Start() and
- not including End().
- */
-
- class Data
- {
- private:
- uint8 *start;
- uint8 *end;
-
- public:
- Data() : start(0), end(0) {}
-
- Data( void *theStart, void *theEnd )
- : start( static_cast<uint8 *>( theStart ) ),
- end( static_cast<uint8 *>( theEnd ) )
- {
- Assert( theEnd >= theStart );
- Assert( theStart != 0 || theEnd == 0 );
- }
-
- Data( void *theStart, uint32 theLength )
- : start( static_cast<uint8 *>( theStart ) ),
- end( static_cast<uint8 *>( theStart ) + theLength )
- {
- Assert( theStart != 0 || theLength == 0 );
- }
-
- Data( ArrayOf<uint8> array )
- : start( array.Start() ),
- end( array.End() )
- {}
-
- uint8 *Start() const { return start; }
- uint8 *End() const { return end; }
-
- uint32 Length() const { return uint32( end - start ); }
-
- uint32 BoundedLength( uint32 bound ) const { return ( Length() <= bound ) ? Length() : bound; }
-
- bool IsEmpty() const { return start >= end; }
- bool Null() const { return start == 0; }
-
- inline uint8& operator[]( uint32 i ) const;
-
- const Data Head( uint32 position ) const { Assert( position <= Length() ); return Data( start, start+position ); }
- const Data Tail( uint32 position ) const { Assert( position <= Length() ); return Data( start+position, end ); }
- const Data Middle( uint32 s, uint32 e ) const { Assert( s <= e ); Assert( e <= Length() ); return Data( start+s, start+e ); }
- const Data Middle( URange32 r ) const { Assert( r.End() <= Length() ); return Data( start+r.Start(), start+r.End() ); }
-
- void Truncate( uint32 position ) { Assert( position <= Length() ); end = start + position; }
- void LimitLength( uint32 limit ) { if ( limit < Length() ) end = start + limit; }
-
- void Shorten( uint32 amount ) { Assert( amount <= Length() ); start += amount; }
- void Lengthen( uint32 amount ) { end += amount; }
-
- operator ConstData() const { return ConstData( start, end ); }
-
- bool StartsWith( ConstData d ) const { return ConstData( start, end ).StartsWith( d ); }
- bool EndsWith( ConstData d ) const { return ConstData( start, end ).EndsWith( d ); }
- bool Contains( ConstData d ) const { return ConstData( start, end ).Contains( d ); }
-
- bool Contains( const void *p ) const { return start <= p && p < end; }
-
- const Data operator<<( ConstData r ) const; // returns unused space, so chaining works
- };
-
- class DataOverflow {};
-
- inline uint8& Data::operator[]( uint32 i ) const
- {
- if ( i >= Length() )
- {
- Assert( 0 );
- throw DataOverflow();
- }
-
- return start[i];
- }
-
- #endif
-